package com.vrv.common.view;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils.TruncateAt;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.TableLayout;
import android.widget.TableLayout.LayoutParams;
import android.widget.TableRow;
import android.widget.TextView;

import com.vrv.common.util.StringUtils;
import com.vrv.common.util.ViewUtils;
import com.vrv.netmgr.R;

/**
 * @Description 公共详情类
 * @author 曹保利
 * @date 2013-08-26 15:00:00
 */
public class CommonDetailActivity extends Activity {

    // 表格布局
    private TableLayout tableLayout;
    // 标题
    private String caption;
    // 标题
    private List<String> labels = null;
    // 标题
    private List<String> values = null;
    // 第1列的最大宽度
    protected int column1MaxWidth;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.common_detail);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.common_title);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        
        TextView title = (TextView) findViewById(R.id.title_text);
        ImageButton backBtn = (ImageButton) findViewById(R.id.backBtn);
        backBtn.setVisibility(View.VISIBLE);
        backBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        findViews();

        caption = getIntent().getStringExtra("caption");
        labels = getIntent().getStringArrayListExtra("labels");
        values = getIntent().getStringArrayListExtra("values");

        title.setText(caption);

        if (labels == null) {
            labels = new ArrayList<String>();
        }
        if (values == null) {
            values = new ArrayList<String>();
        }

        TableRow row = null;
        TextView labelText = null;
        TextView valueText = null;

        initColumn1MaxWidth(labels.size());
        tableLayout.setColumnShrinkable(1, true);

        for (int i = 0; i < labels.size(); i++) {
            String label = String.valueOf(labels.get(i));
            String value = String.valueOf(values.get(i));
            if (StringUtils.isNotBlank(label)) {

                labelText = getTextViewLabel();
                valueText = getTextViewValue();
                labelText.setText(label + ":");
                valueText.setText(value);

                row = new TableRow(this);
                row.setHorizontalGravity(Gravity.LEFT);
                row.addView(labelText);
                row.addView(valueText);
                this.tableLayout.addView(row);
                if (labels.size() > 1 && i < labels.size() - 1) {
                    this.tableLayout.addView(getSegmentLine(), getLayoutParams());
                }
            }
        }
    }

    /**
     * 标题
     *
     * @return
     */
    private TextView getTitleTextView() {
        TextView text = new TextView(this);
        text.setPadding(5, 5, 5, 5);
        text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20L);
        text.setSingleLine(true);
        text.getPaint().setFakeBoldText(true);
        text.setTextColor(Color.BLACK);
        if (StringUtils.isNotBlank(caption)) {
            text.setText(caption);
        } else {
            text.setText("详情信息");
            return null;
        }
        return text;
    }

    /**
     * 列名Label
     */
    private TextView getTextViewLabel() {
        final TextView text = new TextView(this);
        // text.setPadding(1, 0, 0, 0);
        text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16L);
        text.setTextColor(Color.BLACK);
        text.setGravity(Gravity.RIGHT);
        text.setWidth(column1MaxWidth);
        text.setMinWidth(50);
        text.getPaint().setFakeBoldText(true);

        return text;
    }

    /**
     * 列的值
     */
    private TextView getTextViewValue() {
        final TextView text = new TextView(this);
        text.setTextColor(Color.BLACK);
        // text.setEllipsize(TextUtils.TruncateAt.valueOf("END"));
        text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16L);
        text.setEllipsize(TruncateAt.END);
        // text.setSingleLine(true);
        // text.setWidth(tableLayout.getWidth() - column1MaxWidth);
        return text;
    }

    /**
     * 获取标签列最大的宽度
     *
     * @param count
     */
    private void initColumn1MaxWidth(int count) {
        for (int i = 0; i < count; i++) {
            String label = String.valueOf(labels.get(i));
            column1MaxWidth = Math.max(column1MaxWidth, ViewUtils.getStringWidthPixel(label, this));
        }
    }

    /**
     * 查找控件
     */
    private void findViews() {
        tableLayout = (TableLayout) this.findViewById(R.id.detail_tablelayout);
    }

    /**
     * 分割线
     *
     * @return
     */
    private TextView getSegmentLine() {
        TextView text = new TextView(this);
        text.setBackgroundColor(Color.TRANSPARENT);
        text.setHeight(ViewUtils.dip2px(getApplicationContext(), 1));
        text.setBackgroundColor(getResources().getColor(R.color.splite_line_color));
        return text;
    }

    /**
     * 获取布局参数
     *
     * @return
     */
    private LayoutParams getLayoutParams() {
        return getLayoutParams(1);
    }

    /**
     * 获取布局参数
     *
     * @param height
     *            高度
     * @return
     */
    @SuppressWarnings("deprecation")
    private LayoutParams getLayoutParams(int height) {
        return new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, height);
    }

}
